<template>
    <div class="app-container">
      <projectHeader @tabClick="tabClick"></projectHeader>
        <el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch">
          <el-form-item label="所属厂商" prop="manufacturerType">
            <el-select
                v-model="queryParams.manufacturerType"
                placeholder="请选择所属厂商"
                clearable
            >
              <el-option
                  v-for="dict in dict.type.equip_type_manufact"
                  :key="dict.value"
                  :label="dict.label"
                  :value="Number(dict.value)"
              />
            </el-select>
          </el-form-item>
          <el-form-item label="名称" prop="name">
              <el-input
                  v-model="queryParams.name"
                  placeholder="请输入名称"
                  clearable
                  style="width: 200px"
                  @keyup.enter="handleQuery"
              />
          </el-form-item>
          <el-form-item label="协议类型" prop="protocolType">
            <el-select
                v-model="queryParams.protocolType"
                placeholder="请选择协议类型"
                clearable
            >
              <el-option
                  v-for="dict in dict.type.equip_xytype"
                  :key="dict.value"
                  :label="dict.label"
                  :value="dict.value"
              />
            </el-select>
          </el-form-item>
            <!-- <el-form-item label="创建时间" style="width: 308px;">
                <el-date-picker
                    v-model="dateRange"
                    value-format="YYYY-MM-DD"
                    type="daterange"
                    range-separator="-"
                    start-placeholder="开始日期"
                    end-placeholder="结束日期"
                ></el-date-picker>
            </el-form-item> -->
            <el-form-item>
                <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
                <el-button icon="Refresh" @click="resetQuery">重置</el-button>
            </el-form-item>
        </el-form>

        <el-row :gutter="10" class="mb8">
            <el-col :span="1.5">
                <el-button
                    type="primary"
                    plain
                    icon="Plus"
                    @click="handleAdd"
                    v-hasPermi="['system:eaCarChargingType:add']"
                >新增
                </el-button>
            </el-col>
           <!-- <el-col :span="1.5">
                <el-button
                    type="danger"
                    plain
                    icon="Delete"
                    :disabled="multiple"
                    @click="handleDelete"
                    v-hasPermi="['system:eaCarChargingType:remove']"
                >删除
                </el-button>
            </el-col> -->
           <!-- <el-col :span="1.5">
                <el-button
                    type="warning"
                    plain
                    icon="Download"
                    @click="handleExport"
                    v-hasPermi="['system:eaCarChargingType:export']"
                >导出
                </el-button>
            </el-col> -->
            <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
        </el-row>

        <el-table v-loading="loading" :data="list" @selection-change="handleSelectionChange">
            <!-- <el-table-column type="selection" width="55" align="center"/> -->
            <el-table-column label="编号" width="60" align="center" prop="id"/>
            <el-table-column label="所属厂商" align="center" prop="manufacturerType">
              <template slot-scope="{row}">
                <dict-tag :options="dict.type.equip_type_manufact" :value="row.manufacturerType" />
              </template>
            </el-table-column>
            <el-table-column label="名称" align="center" prop="name"/>
            <el-table-column label="协议类型" align="center" prop="protocolType">
              <template slot-scope="{row}">
                <dict-tag :options="dict.type.equip_xytype" :value="row.protocolType" />
              </template>
            </el-table-column>
            <el-table-column label="额定电压" align="center" prop="ratedVoltage"/>
            <el-table-column label="额定功率" align="center" prop="ratedPower"/>
						<el-table-column label="额定电流" align="center" prop="ratedCurrent"/>
            <el-table-column label="创建时间" align="center" prop="createTime" width="180">
                <template slot-scope="{row}">
                    <span>{{ parseTime(row.createTime) }}</span>
                </template>
            </el-table-column>
            <el-table-column label="操作" width="180" align="center" class-name="small-padding fixed-width">
                <template slot-scope="{row}">
                    <el-button type="text" icon="el-icon-edit" @click="handleUpdate(row)"
                               v-hasPermi="['system:eaCarChargingType:edit']">修改
                    </el-button>
                    <el-button type="text" icon="el-icon-delete" @click="handleDelete(row)"
                               v-hasPermi="['system:eaCarChargingType:remove']">删除
                    </el-button>
                </template>
            </el-table-column>
        </el-table>

        <pagination
          v-show="total>0"
          :total="total"
          :page.sync="queryParams.pageNum"
          :limit.sync="queryParams.pageSize"
          @pagination="getList"
        />

        <!-- 添加或修改设备类型对话框 -->
        <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
            <el-form ref="postRef" :model="form" :rules="rules" label-width="auto">
              <el-form-item label="所属厂商" prop="manufacturerType">
                <el-select
                    v-model="form.manufacturerType"
                    placeholder="请选择所属厂商"
                    clearable
                >
                  <el-option
                      v-for="dict in dict.type.equip_type_manufact"
                      :key="dict.value"
                      :label="dict.label"
                      :value="Number(dict.value)"
                  />
                </el-select>
              </el-form-item>
                <el-form-item label="名称" prop="name">
                  <el-input v-model="form.name" placeholder="请输入名称"/>
                </el-form-item>
                <el-form-item label="	端口数量" prop="portNumber">
                  <el-input-number v-model="form.portNumber" :min="0" step-strictly :step="1"></el-input-number>
                </el-form-item>
                <el-form-item label="额定电压" prop="ratedVoltage">
                    <el-input v-model="form.ratedVoltage" placeholder="请输入额定电压"/>
                </el-form-item>
                <el-form-item label="额定电流" prop="ratedCurrent">
                    <el-input v-model="form.ratedCurrent" placeholder="请输入额定功率"/>
                </el-form-item>
                <el-form-item label="额定功率" prop="ratedPower">
                    <el-input v-model="form.ratedPower" placeholder="请输入额定功率"/>
                </el-form-item>
                <el-form-item label="协议类型" prop="protocolType">
                  <el-select
                      v-model="form.protocolType"
                      placeholder="请选择协议类型"
                      clearable
                  >
                    <el-option
                        v-for="dict in dict.type.equip_xytype"
                        :key="dict.value"
                        :label="dict.label"
                        :value="dict.value"
                    />
                  </el-select>
                </el-form-item>
                <el-form-item label="设备类型" prop="deviceType">
                  <el-radio-group v-model="form.deviceType">
                    <el-radio
                    v-for="(item,index) in dict.type.charg_device_type" :key="index"
                    :label="Number(item.value)">{{item.label || ''}}</el-radio>
                  </el-radio-group>
                </el-form-item>
               <!-- <el-form-item label="图片" prop="image">
                    <imageUpload v-model="form.image" :limit="1"></imageUpload>
                </el-form-item> -->
            </el-form>
            <template slot="footer" class="dialog-footer">
              <el-button type="primary" @click="submitForm">确 定</el-button>
              <el-button @click="cancel">取 消</el-button>
            </template>
        </el-dialog>

    </div>
</template>

<script>
import {
    EXPORT_URL,
    addDeviceType,
    delDeviceType,
    listDeviceType,
		getDeviceType,
    updateDeviceType
} from "@/api/station/device_type";
import {setForm,parseTime} from "@/utils/ruoyi";
export default {
  name: 'DeviceType',
  components: {  },
  dicts:['equip_type_manufact','equip_xytype','charg_device_type'],
  data() {
    return {
      form: {},// 表单参数
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      queryParams: {// 查询参数
        pageNum: 1,
        pageSize: 10,
        name: undefined,
        voltage: undefined,
        power: undefined,
      },
      // 总条数
      total: 0,
      rules: {
        name: [{required: true, message: "名称不能为空", trigger: "blur"}],
        ratedVoltage: [{required: true, message: "额定电压不能为空", trigger: "blur"}],
        ratedCurrent: [{required: true, message: "额定电流不能为空", trigger: "blur"}],
        ratedPower: [{required: true, message: "额定功率不能为空", trigger: "blur"}],
        portNumber: [{required: true, message: "端口数量不能为空", trigger: "blur"}],
        manufacturerType: [{required: true, message: "请选择所属厂商", trigger: "change"}],
        protocolType: [{required: true, message: "请选择协议类型", trigger: "change"}],
      },
      operatorOptions:[],//所属运营商
      list:[],//页面数据
      dateRange:[],
    };
  },
  mounted(){
    // this.getList()
  },
  methods: {
    tabClick(e){//平台切换
      this.plateform = {...e}
      this.queryParams.pageNum = 1
      this.getList()
    },
    getList() {/** 查询列表 */
      this.loading = true;
      this.queryParams.platformType = this.plateform.value
      listDeviceType(this.queryParams).then(response => {
          this.list = response.rows;
          this.total = response.total;
          this.loading = false;
      });
    },
    cancel() {/** 取消按钮 */
      this.open = false;
      this.reset();
    },
    reset() {/** 表单重置 */
      this.form = {
          id: undefined,
          name: undefined,
          voltage: undefined,
          power: undefined,
          current: undefined,
          image: undefined,
      };

      this.resetForm("postRef");
    },
    handleQuery() {/** 搜索按钮操作 */
      this.queryParams.pageNum = 1;
      this.getList();
    },
    resetQuery() {/** 重置按钮操作 */
      this.dateRange = [];
      this.resetForm("queryRef");
      this.handleQuery();
    },
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id);
      this.single = selection.length != 1;
      this.multiple = !selection.length;
    },
    handleAdd() {
      this.open = true;
      this.reset();
      this.title = "添加设备类型";
    },
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids;
      this.open = true;
      this.title = "修改设备类型";
      getDeviceType(id).then(response => {
        let data = response.data || {}
        this.form = {...data,};
      });
    },
    submitForm() {
      this.$refs["postRef"].validate(valid => {
        if (valid) {
          let form = {...this.form}
          form.platformType = this.plateform.value
          if (this.form.id != undefined) {
            form = setForm(form, 'type_');
            updateDeviceType(form).then(response => {
              this.$modal.msgSuccess("修改成功")
              this.open = false;
              this.getList();
            });
          } else {
            addDeviceType(form).then(response => {
              this.$modal.msgSuccess("新增成功")
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    handleDelete(row) {//删除
      const del_ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除编号为"' + del_ids + '"的数据项？').then(function() {
        return delDeviceType(del_ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    handleExport() {
      this.download(EXPORT_URL, {
          ...this.queryParams
      }, `device_type_${new Date().getTime()}.xlsx`);
    },
    handleUploadSuccess(value) {
      // console.log('handleUploadSuccess', value)
      this.form['image'] = value;
    },
  }
}
</script>
